package com.yf.dxapp.module.app.controller.admin.appbanner;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import com.yf.dxapp.module.app.controller.admin.appbanner.vo.AppBannerPageReqVO;
import com.yf.dxapp.module.app.controller.admin.appbanner.vo.AppBannerRespVO;
import com.yf.dxapp.module.app.controller.admin.appbanner.vo.AppBannerSaveReqVO;
import com.yf.dxapp.module.app.dal.dataobject.appbanner.AppBannerDO;
import com.yf.dxapp.module.app.service.appbanner.AppBannerService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 广告图")
@RestController
@RequestMapping("/dxapp/app-banner")
@Validated
public class AppBannerController {

    @Resource
    private AppBannerService appBannerService;

    @PostMapping("/create")
    @Operation(summary = "创建广告图")
    @PreAuthorize("@ss.hasPermission('t:app-banner:create')")
    public CommonResult<Long> createAppBanner(@Valid @RequestBody AppBannerSaveReqVO createReqVO) {
        return success(appBannerService.createAppBanner(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新广告图")
    @PreAuthorize("@ss.hasPermission('t:app-banner:update')")
    public CommonResult<Boolean> updateAppBanner(@Valid @RequestBody AppBannerSaveReqVO updateReqVO) {
        appBannerService.updateAppBanner(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除广告图")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('t:app-banner:delete')")
    public CommonResult<Boolean> deleteAppBanner(@RequestParam("id") Long id) {
        appBannerService.deleteAppBanner(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得广告图")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('t:app-banner:query')")
    public CommonResult<AppBannerRespVO> getAppBanner(@RequestParam("id") Long id) {
        AppBannerDO appBanner = appBannerService.getAppBanner(id);
        return success(BeanUtils.toBean(appBanner, AppBannerRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得广告图分页")
    @PreAuthorize("@ss.hasPermission('t:app-banner:query')")
    public CommonResult<PageResult<AppBannerRespVO>> getAppBannerPage(@Valid AppBannerPageReqVO pageReqVO) {
        PageResult<AppBannerDO> pageResult = appBannerService.getAppBannerPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AppBannerRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出广告图 Excel")
    @PreAuthorize("@ss.hasPermission('t:app-banner:export')")
    
    public void exportAppBannerExcel(@Valid AppBannerPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AppBannerDO> list = appBannerService.getAppBannerPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "广告图.xls", "数据", AppBannerRespVO.class,
                        BeanUtils.toBean(list, AppBannerRespVO.class));
    }

}